# -*- coding:utf-8 -*-
"""
pyecharts 调用
"""
from functools import reduce
from os import close
from numpy.core.numeric import NaN
from numpy.lib.function_base import append
import pandas as pd
from echarts.HandleChartsView import ChartsView
from hander.HanderZnzView import HanderZnzView
from _cfg import base as cfg
import math
from dingding import dingding
import pandas_ta as ta


def echarts_gen():
    index_0_table = 'index_0'
    index_0a_table = 'index_0a'
    index0_data = ''  # 0号指标DF数据
    index0a_data = ''  # 0A指标DF数据

    num_ = 20000  # 每页数据
    # page=1 #页码

    hzv_obj = HanderZnzView()
    # 创建数据库连接
    hzv_obj.create_sql_con(cfg=cfg)
    # 0号指数和0B指数
    count_data_index0 = hzv_obj.get_data_count(table_name=index_0_table)
    count_num_index0 = count_data_index0.iloc[0, 0]  # 总数据

    # 总页数
    pages_index0 = math.ceil(count_num_index0/num_)
    pages_list_index0 = list(range(1, pages_index0+1))

    for page in pages_list_index0:
        index0_data = hzv_obj.get_data_from_sql(
            table_name=index_0_table, pages=page, num_=num_, desc=False)

    index0b_data = index0_data.copy(deep=True)
    
    index0b_data.drop('open_0', axis=1, inplace=True)
    index0b_data.drop('high_0', axis=1, inplace=True)
    index0b_data.drop('low_0', axis=1, inplace=True)
    index0b_data.drop('close_0', axis=1, inplace=True)
    index0b_data.drop('vol_0', axis=1, inplace=True)
    index0b_data.drop('amount_0', axis=1, inplace=True)
   
    # 0A指数
    count_data_index0a = hzv_obj.get_data_count(table_name=index_0a_table)
    count_num_index0a = count_data_index0a.iloc[0, 0]  # 总数据

    # 总页数
    pages_index0a = math.ceil(count_num_index0a/num_)
    pages_list_index0a = list(range(1, pages_index0a+1))

    for page in pages_list_index0a:
        index0a_data = hzv_obj.get_data_from_sql(
            table_name=index_0a_table, pages=page, num_=num_, desc=False)

    chart_obj = ChartsView(index0data=index0_data,
                           index0adata=index0a_data, index0bdata=index0b_data)
    dingding(title='echarts页面生成信息', news=[
             "Echarts页面生成", "可通过WEB页面访问指标图表，请点击[指标访问]"])  # 发送钉钉消息


if __name__ == "__main__":
    echarts_gen()


    
    """
    
    index_0_table = 'index_0a'
    hzv_obj = HanderZnzView()
    # 创建数据库连接
    hzv_obj.create_sql_con(cfg=cfg)
    # 0号指数和0B指数
    count_data_index0 = hzv_obj.get_data_count(table_name=index_0_table)
    count_num_index0 = count_data_index0.iloc[0, 0]  # 总数据
    num_ = 20000  # 每页数据
    # 总页数
    pages_index0 = math.ceil(count_num_index0/num_)
    pages_list_index0 = list(range(1, pages_index0+1))

    for page in pages_list_index0:
        index0_data = hzv_obj.get_data_from_sql(
            table_name=index_0_table, pages=page, num_=num_, desc=False)
    # kwargs = {"close": index0_data['close_0A'],"append":True}
    # index0_data.ta.macd(**kwargs)
    # index0_data.ta.macd(close='close_0A',append=True)
    # index0_data.fillna(0, inplace=True)
    # index0_data.ta.macd(append=True)
    # print(index0_data.head(100))
    # index0_data.ta.indicators()
    # index0_data.ta.adx(high="high_0A",low="low_0A",close="close_0A",append=True)
    # index0_data['ADXR_14']=[]
    # adxr_value=[]
    # day_out=6
    # for i in index0_data.index:
    #     if(i-day_out) <0:
    #         adxr_value.append(NaN)
    #     else:
    #         if index0_data['ADX_14'][i-day_out] == NaN:
    #             adxr_value.append(NaN)
    #         else:
    #             adxr_value.append((index0_data['ADX_14'][i]+index0_data['ADX_14'][i-day_out])/2)

    # index0_data['ADXR_14']=adxr_value
    # mal = index0_data.ta.sma(close='close_0A', length=5)
    # ma20 = index0_data.ta.sma(close='close_0A', length=20)
    # # print(ma20[1])
    # mas = index0_data.ta.sma(close=ma20, length=5)
    # cyel_value = []
    # cyes_value = []
    # for i in mal.index:
    #     if (i-1) < 0:
    #         cyel_value.append(NaN)
    #     else:
    #         if mal[i-1] == NaN:
    #             cyel_value.append(NaN)
    #         else:
    #             cyel_value.append(
    #                 ((mal[i]-mal[i-1]) / mal[i-1])*100)

    # for i in mal.index:
    #     if (i-1) < 0:
    #         cyes_value.append(NaN)
    #     else:
    #         if mas[i-1] == NaN:
    #             cyes_value.append(NaN)
    #         else:
    #             cyes_value.append(
    #                 ((mas[i]-mas[i-1]) / mas[i-1])*100) 
    # index0_data['CYEL']=cyel_value
    # index0_data['CYES']=cyes_value

    # print(index0_data.head(50))       

    # print(ma20)
    # print(adxr_value)

    # CYC 5,13,34
    # 0.01*EMA(AMOUNT,P1)/EMA(VOL,P1)
    # ema5_amount=index0_data.ta.ema(close="amount_0",length=5)
    # ema5_vols=index0_data.ta.ema(close="vol_0",length=5)
    # cyc_5=[]
    # for i in ema5_amount.index:
    #     if ema5_amount[i] == NaN:
    #         cyc_5.append(NaN)
    #     else:
    #         cyc_5.append(ema5_amount[i]/ema5_vols[i])
    
    # index0_data['cyc_5']=cyc_5

    # print(index0_data.head(100))
    
    # DMA(AMOUNT/(100*VOL),VOL/FINANCE(7))    FINANCE(7) 流通股本
    
    # cyc_inf=[]
    # index0_data['X_']=index0_data.apply(lambda x: x['amount_0']/x['vol_0'],axis=1)
    # index0_data['A_']=index0_data.apply(lambda x: x['vol_0']/x['tq_0'],axis=1)

    # pd_=index0_data[['X_','A_']]

    # # print(pd_.head(5))

    # for i in index0_data.index:
    #     X=pd_['X_'].head(i+1).values.tolist()
    #     A=pd_['A_'].head(i+1).values.tolist()
    #     inf_=reduce(lambda x, y: (1 - y[1]) * x + y[1] * y[0], zip(X, A), X[0])
    #     print(X[0])
    #     cyc_inf.append(inf_)
    
    # print(cyc_inf) 
    
    index0_data.ta.sma(close='amount_0',length=10,offset=1,prefix="0amv",append=True)
    print(index0_data.head(100))
    # print(index0_data.columns)
    """